package com.yk.anima.servlet;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yk.anima.dao.ProductsDirdsMapper;
import com.yk.anima.pojo.ProductsDirds;
import com.yk.anima.response.CommonCode;
import com.yk.anima.response.QueryResponseResult;
import com.yk.anima.response.QueryResult;
import com.yk.anima.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaokai
 * @create 2021-05-14 19:12:38
 * @desc...对鸟的业务进行管理
 */

@Service
public class ProductsDirdsServlet {

    @Autowired
    ProductsDirdsMapper productsDirdsMapper;


    public List<ProductsDirds> findAll() {
        List<ProductsDirds> list = productsDirdsMapper.findAll();
        if (list.size() == 0) {
            return null;
        }
        return list;
    }

    public ProductsDirds findById(int id) {
        ProductsDirds productsDirds = productsDirdsMapper.selectByPrimaryKey(id);
        if (productsDirds == null) {
            return null;
        }
        return productsDirds;
    }

    public ResponseResult update(int id, ProductsDirds productsDirds) {
        ProductsDirds dirds = productsDirdsMapper.selectByPrimaryKey(id);
        if (dirds == null) {
            return new ResponseResult(CommonCode.FINDALLANDNULL);
        }
        dirds.setAge(productsDirds.getAge());
        dirds.setDescription(productsDirds.getDescription());
        dirds.setGender(productsDirds.getGender());
        dirds.setName(productsDirds.getName());
        dirds.setPrice(productsDirds.getPrice());
        dirds.setBirdsId(2);
        int number = productsDirdsMapper.updateByPrimaryKeySelective(dirds);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public ResponseResult save(ProductsDirds productsDirds) {
        if (productsDirds==null){
            productsDirds=new ProductsDirds();
        }
        productsDirds.setBirdsId(2);
        productsDirds.setpId(null);
        int number = productsDirdsMapper.insert(productsDirds);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public ResponseResult deleteById(int id) {
        int number = productsDirdsMapper.deleteByPrimaryKey(id);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public QueryResponseResult findByPage( int page,int size, String name) {
        if (size <= 0) {
            size = 2;
        }
        if (page <= 0) {
            page = 1;
        }

        PageHelper.startPage(page, size);
        Page<ProductsDirds> productsDirds = productsDirdsMapper.findByPage(name);
        long total = productsDirds.getTotal();
        List<ProductsDirds> result = productsDirds.getResult();
        QueryResult<ProductsDirds> queryResult = new QueryResult<>();
        queryResult.setList(result);
        queryResult.setTotal(total);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);

    }
}
